package com.leaves.javamall.product.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.leaves.javamall.product.entity.AttrEntity;
import com.leaves.javamall.product.service.AttrService;
import com.leaves.javamall.product.vo.AttrGroupRelationVo;
import com.leaves.javamall.product.vo.AttrGroupWithAttrsVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.Query;

import com.leaves.javamall.product.dao.AttrGroupDao;
import com.leaves.javamall.product.entity.AttrGroupEntity;
import com.leaves.javamall.product.service.AttrGroupService;

import javax.jws.Oneway;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //判断前端传来的搜索关键字
        String key = (String) params.get("key");
        //我们想要实现的sql语句
        //select * from pms_attr_group where attr_group_id=categroyId And (attr_group_id=key or attr_group_id like %key%)
        //新建一个新的搜索条件查询wrapper
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(categoryId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id",categoryId);
            //如果搜索关键字不为空，我们可以将其拼成串，交给sql查询
            if(!StringUtils.isNullOrEmpty(key)){
                wrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            //使用工具类将其封装，返回
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    //根据分类Id查出所有的分组以及组内属性
    @Override
    public List<AttrGroupRelationVo> getgetAttrGroupWithAttrsByCatelogId(Long catId) {
        //查询分组信息
        List<AttrGroupEntity> ids = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        //查询所有属性
        List<AttrGroupWithAttrsVo> collect = ids.stream().map(group -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrGroupWithAttrsVo);
            List<AttrEntity> attr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());


        return null;
    }

}